// 路由规则匹配模块 负责定义路由匹配规则
import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/users.vue'
import rights from '../components/powers/rights.vue'
import roles from '../components/powers/roles.vue'
import categories from '../components/goods/categories.vue'
import params from '../components/goods/params.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    { path: '/home', component: home, redirect: '/welcome', children: [{ path: '/home', component: welcome }, { path: '/welcome', component: welcome }, { path: '/rights', component: rights }, { path: '/users', component: users }, { path: '/roles', component: roles }, { path: '/categories', component: categories }, { path: '/params', component: params }] }
  ]
})

router.beforeEach((to, from, next) => { // 路由守卫 判断输入的地址不为login 时的token值 to 是要去的页面 ，from是从哪个页面跳的
  if (to.path === '/login') return next() // 输入路径为 login  直接放行
  const tokenStr = localStorage.getItem('token') // 获取本地的token
  if (!tokenStr) return next('/login') // 如果本地没有token 跳转到login 界面
  next() // 都不符合代表正常访问
})
export default router
